{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "700adddd-3ba0-4c0b-8f00-016b450a79d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q pyiceberg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6d0a63a-a898-41ce-a68f-932fd4d5383f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyiceberg.catalog.rest import RestCatalog\n",
    "import logging\n",
    "\n",
    "import pandas as pd\n",
    "import pyarrow.parquet as pq\n",
    "import pyarrow as pa\n",
    "from device_code_auth import authenticate_device_flow_with_pkce\n",
    "\n",
    "CATALOG_URL = \"http://lakekeeper:8181/catalog\"\n",
    "WAREHOUSE = \"demo\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67be2b7a-5a9e-44de-b9a3-779da4489d81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Login as user \"Peter\" or \"Anna\n",
    "auth_response = authenticate_device_flow_with_pkce(\n",
    "    token_endpoint=\"http://keycloak:8080/realms/iceberg/protocol/openid-connect/token\",\n",
    "    device_endpoint=\"http://keycloak:8080/realms/iceberg/protocol/openid-connect/auth/device\",\n",
    "    client_id=\"lakekeeper\",\n",
    "    scope=\"lakekeeper\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff8c5825-d45a-49fa-8d3a-4b1271ec7469",
   "metadata": {},
   "outputs": [],
   "source": [
    "catalog = RestCatalog(\n",
    "    name=\"demo\",\n",
    "    warehouse=\"demo\",\n",
    "    uri=\"http://lakekeeper:8181/catalog/\",\n",
    "    token=auth_response[\"access_token\"],\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd38e025-c6e2-4274-a06c-2aaf7847c5c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read data\n",
    "table = catalog.load_table(\"finance.products\")\n",
    "table.scan().to_pandas()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60ce7b61-6bc7-4065-ac7f-095ee03721f2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
